﻿
var actionFlag = getPar1("actionFlag");
var const_UserId = parent.parent._currentUserID;
var const_UserName = parent.parent._currentUserName;

$(document).ready(function () {
    if (actionFlag == 1) {
        var amountObj = $("#Amount");
        amountObj.numberbox({
            min: -999999999,
            precision: 2
        });

        $("#AddUserName").val(const_UserName);
        now = new Date();
        $("#AddTime").val(fnGetMyDate(now));
    }
});


var getCompanyData = function (company) {
    return fnGetData("BH_Company", "CompanyNo='" + company + "' or CompanyName='" + company + "' or ShortName='" + company + "'");
}

// 设置供应商名称
var setCompanyName = function () {
    var companyNo = $("#VendorNo").attr("SValue");
    Company = getCompanyData(companyNo);

    if (Company == null || Company == "") {
        alert("厂家：" + companyNo + " 还未定义!");
        $("#VendorName").val("");
        $("#VendorNo").focus();
        return;
    }

    $("#VendorName").val(Company.rows[0].CompanyName);
}


// 提交检测
var fnCustomCheck = function () {
    if (instance[0].VendorNo == "" || instance[0].VendorNo == null) {
        $('#VendorNo').focus();
        return "客户不能为空!";
    }
    if (instance[0].EntryDate == "" || instance[0].EntryDate == null) {
        $('#EntryDate').focus();
        return "应付日期不能为空!";
    }
    if (instance[0].Category == "" || instance[0].Category == null) {
        $('#Category').focus();
        return "应付科目不能为空!";
    }
    if (parseFloat(instance[0].Amount) == 0 || instance[0].Amount == null) {
        $('#Amount').focus();
        return "应付金额不能为0!";
    }

    return "";
}

var fnSetValue = function () {
    instance[0].AddUser = const_UserId;
}

// 保存数据
var fnCustomSave = function () {

    fnSetValue();

    dataAction = "A";

    json = $.toJSON(instance[0]).replace(/'/g, "[B]").replace(/\+/g, "[J]");
    //debugger;
    //return;
    $.ajax({
        async: false, //异步请求
        cache: false, //不从浏览器缓存中加载
        type: "GET",
        /*dataType: "json",*/
        url: "/DCBH/DCBHCommonOperator.aspx",
        data: { Action: "procedure", entity: "V_BH_AccountsPayableDetail", json: json, detailJson: "", dataAction: dataAction },
        success: function (data) {
            //debugger;
            if (data != "") {
                if (data == "1") {
                    alert("数据保存成功!");

                    if (parent.SaveAndAddNewFlag == true) {
                        location.href = location.href
                    }

                    if (parent.SaveSuccessClose == true) {
                        parent.parent.CloseCurPage();
                    }
                }
                else {
                    alert(data);
                }
            }
        },
        error: function (e) {
            alert("保存失败, 请联系开发人员，核查系统！");
            return;
        }
    });

}